---
title: "Matrix Algebra in R"
author: "SickKids and DARTH"
output:
  pdf_document: default
  html_document:
    df_print: paged
subtitle: Introduction
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, eval = FALSE)
```

Change `eval` to `TRUE` if you want to knit this document.

This worksheet provides an introduction to matrix algebra in `R`:

Throughout the course, we will demonstrate code and leave some empty *code chunks* for you to fill in. We will also provide solutions after the session.

Feel free to modify this document with your own comments and clarifications. 

**EXERCISE 1** Use the following *code chunk* to create a matrix `matrixB` with 2 rows and 4 columns and a matrix `matrixC` with 4 rows and 2 columns:

NOTE: you may decide what the elements are for both matrices - they can be anything as long as the dimensions are right.

```{r}
# Your turn

```

In `R`, there are three key operations that we can do with matrices, addition (`+`), multiplication (`*`) and matrix multiplication (`%*%`). Each of these operations can only be used if the matrices are compatible.

**EXERCISE 2** Perform the following operations on `m_A` and `m_B`:

1) Their sum

2) Their element-wise product (the Hadamard product)

3) Their product

4) The product of the first matrix with the transpose of the second matrix

```{r}
m_A <- matrix(1:9,  nrow = 3, ncol = 3)
m_B <- matrix(6:14, nrow = 3, ncol = 3)
```

```{r}
# Your turn

```

**EXERCISE 3** We created two matrices, `matrix1` and `matrix2` in the code chunk below. Perform the following operations and display the results:

1) Add `matrix1` to `matrix2` and take the transpose of the sum, and matrix-multiply the result with `matrix2`

2) Obtain the transpose of the product of `matrix1` and `matrix2` and element-wise multiply the result with `matrix1`

```{r}
matrix1 <- matrix(1:9, nrow = 3, ncol = 3)
matrix2 <- matrix(c(1, 0, 0, 0, 3, 4, 5, 9, 2), nrow = 3, ncol = 3)

# 1.
# Your turn

# 2.
# Your turn

```


